package org.example.topicdata;

import org.example.ConnectDatabase;
import org.example.Server;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Random;

public class GetTopics extends ConnectDatabase {
    public int createHeadTopics(){
        new ConnectDatabase();
        Statement sql;
        ResultSet rs;
        Topic result;
        String str = "select * from Topics";
        Random random ;
        int imf = 0;
        try {
            sql = con.createStatement();
            rs = sql.executeQuery(str);
            while (rs.next()) {
                result = new Topic();
                result.setID(rs.getInt(1));
                result.setTopicName(rs.getString(2).trim());
                result.setSelect_A(rs.getString(3).trim());
                result.setSelect_B(rs.getString(4).trim());
                result.setSelect_C(rs.getString(5).trim());
                result.setSelect_D(rs.getString(6).trim());
                result.setAns(rs.getString(7).trim());
                int imf_1 = 0;
                for (int i = 0; i < Server.topicarrayList.size(); i++) {
                    if (Server.topicarrayList.get(i).ID != result.ID){
                        imf_1++;
                    }else break;
                }
                if (imf_1 == Server.topicarrayList.size())
                    Server.topicarrayList.add(result);
                imf ++;
            }
            con.close();
        }catch (SQLException e){}
        random = new Random();
        int head = random.nextInt(Server.topicarrayList.size());
        if (head > Server.topicarrayList.size() - 5) head = Server.topicarrayList.size() - 5;
        return head;
    }
}
